package com.shwread.android.ui.widget;

import java.util.HashMap;
import java.util.Map;

import logic.util.ScreenUtil;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;

public class SelectCursorPopup {

	private static PopupWindow popup;
	private static SelectCursorPopup SelectPopup;
	private static Context mContext;
	private static Map<Byte, PopupItem> Views;
	private static byte CurrName;
	private static boolean mDismissListener;

	private SelectCursorPopup(Context context) {
		Views = new HashMap<Byte, SelectCursorPopup.PopupItem>();
		popup = new PopupWindow(context);
		popup = new PopupWindow(context);
		popup.setBackgroundDrawable(new BitmapDrawable());
		popup.setOutsideTouchable(true);
		popup.setFocusable(true);
		//隐藏事件
		popup.setOnDismissListener(new OnDismissListener() {
			
			@Override
			public void onDismiss() {
				// TODO Auto-generated method stub
				if (mDismissListener) {
					
					mContext.sendBroadcast(new Intent("HideSelect"));
				}
			}
		});

	}

	public static SelectCursorPopup Instance(Context context) {
		if (SelectPopup == null) {
			SelectPopup = new SelectCursorPopup(context);
			mContext = context;
		}
		return SelectPopup;
	}

	public static void AddPopup(byte name, View view, OnClickListener click,boolean dismissListener) {
		if (SelectPopup == null) {
			SelectCursorPopup.Instance(view.getContext());
		}
		if (!Views.containsKey(name)) {
			PopupItem pop = new PopupItem(view, click,dismissListener);
			Views.put(name, pop);
		}
	}

	public void Show(View v, OnClickListener click, int x, int y, int Width,
			int Height, View Root) {
		int count = ((ViewGroup) v).getChildCount();
		for (int i = 0; i < count; i++) {
			((ViewGroup) v).getChildAt(i).setOnClickListener(click);
		}
		MeasureView(v, Width, Height);
		popup.setWidth(Width);
		popup.setHeight(Height);
		popup.setContentView(v);
		popup.showAtLocation(Root, Gravity.LEFT | Gravity.TOP, x, y);

	}

	public static void Show(byte name, int x, int y, int width, int hight,
			View Root) {
		PopupItem pop = Views.get(name);
		if(pop == null) return ;
		mDismissListener = pop.is_DismissListener();
		CurrName = name;
		pop.registerListener();
		MeasureView(pop.mView, width, hight);
		popup.setWidth(width);
		popup.setHeight(hight);
		popup.setContentView(pop.mView);
		popup.showAtLocation(Root, Gravity.LEFT | Gravity.TOP, x, y);
	}
	/**
	 * 显示PopupWindow
	 * @param name 
	 * @param vlaue 屏幕的百分比
	 * @param y   Y坐标
	 * @param width
	 * @param height
	 * @param Root
	 * @param is_key 是否显示键盘
	 * @param Screent_Center 是否在屏幕中间
	 */
	public static void Show(byte name,float value,int y,int height,View Root,boolean is_key,boolean Screent_Center){
		PopupItem pop = Views.get(name);
		if(pop == null) 
			  return;
		mDismissListener = pop.is_DismissListener();
		pop.registerListener();
		int x = get_X(value);
		MeasureView(pop.mView, ScreenUtil.screenWidth-2*x, height);
		popup.setContentView(pop.mView);
		popup.setWidth(ScreenUtil.screenWidth-2*x);
		popup.setHeight(height);
		popup.showAtLocation(Root, Gravity.LEFT | Gravity.TOP, x, y-height);
	}
	
	/**
	 * 
	 * @param name Key
	 * @param value 宽度占屏幕的比例
	 * @param height 高度
	 * @param Root  
	 * @param is_key 是否显示键盘
	 * @param Screent_Center 是否在屏幕中间
	 * @param startY 选择光标的开始
	 * @param endY 选择光标的结束
	 */
	public static void Show(byte name,float value,int height,View Root,boolean is_key,boolean Screent_Center,int startY,int endY){
		PopupItem pop = Views.get(name);
		
		if(pop == null) 
			  return;
		pop.registerListener();
		mDismissListener = pop.is_DismissListener();
		int y = Compute_Y(endY, startY, height);
		int x = get_X(value);
		MeasureView(pop.mView, ScreenUtil.screenWidth-2*x, height);
		popup.setContentView(pop.mView);
		popup.setWidth(ScreenUtil.screenWidth-2*x);
		popup.setHeight(height);
		popup.showAtLocation(Root, Gravity.LEFT | Gravity.TOP, x, y-height);
	}
	
//	public static void Show(byte name,float value,int y,int height,View Root,boolean is_key)

	// 隐藏Popup
	public static void Hide() {
		if (popup != null) {
			popup.dismiss();
		}
	}

	public static void Updata(int x, int y) {
		
		popup.update(x, y, popup.getWidth(), popup.getHeight());

	}
	
	
	//Popup是否显示
	public static boolean PopupIs_Show(){
		return popup.isShowing();
	}

	// 测量View大小
	public static void MeasureView(View view, int x, int y) {
		view.setMinimumHeight(x);
		view.setMinimumWidth(y);
	}

	static class PopupItem {
		private View mView;
		private OnClickListener mClickListener;
		private boolean mDismiss ;

		private PopupItem(View view, OnClickListener clicklistener,boolean dismiss) {
			mView = view;
			mClickListener = clicklistener;
			mDismiss = dismiss;
		}
		
		//注册事件
		private void registerListener() {
			int count = ((ViewGroup) mView).getChildCount();
			for (int i = 0; i < count; i++) {
				((ViewGroup) mView).getChildAt(i).setOnClickListener(
						mClickListener);
			}
		}
		
		private boolean  is_DismissListener(){
			
			return mDismiss;
		}

	}
	
	/**
	 * 隐藏键盘
	 * 
	 * @param mInputMethodManager
	 * @param mView
	 */
	public static void hideInputMethodManager(InputMethodManager mInputMethodManager, View mView) {
		if (mInputMethodManager != null && mView != null)
		{
			mInputMethodManager.hideSoftInputFromWindow(mView.getWindowToken(), 0);
		}
	}
	
	//显示键盘
	public static InputMethodManager showInputMethodManager(Context mContext) {
		final InputMethodManager mInputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
		mInputMethodManager.toggleSoftInput(InputType.TYPE_CLASS_PHONE, InputMethodManager.HIDE_NOT_ALWAYS);
		return mInputMethodManager;
	}

	//获取X起点
	public static int get_X(float value){
		int Width = (int) (ScreenUtil.screenWidth*value);
		return (ScreenUtil.screenWidth - Width) /2;
	}
	
	//计算Y坐标
	private static int Compute_Y(int endY,int startY,int Height){
		if ((Height*2+endY) < ScreenUtil.screenHeight) {
			
			return  Height*2+endY;
		}else if(Height*3 < (endY-startY)){
			return (startY+endY+Height)/2;
		}
			return endY - Height;
	}
	
	
	
	
}
